package main
import (
	"fmt"
	"reflect"
)
type order struct {
    ordId      int
    customerId int
}
type employee struct {
	name string
	id int
	address string
	salary int
	country string
}
func createQuery2(q interface{}) {
	if reflect.ValueOf(q).Kind() == reflect.Struct {
		t := reflect.TypeOf(q).Name()
		query := fmt.Sprintf("insert into %s values(", t)
		v := reflect.ValueOf(q)
		for i := 0; i <v.NumField(); i++ {
			switch v.Field(i).Kind() {
			case reflect.Int:
				if i == 0 {
					query = fmt.Sprintf("if Int ==> %s %d", query, v.Field(i).Int())
				} else {
					query = fmt.Sprintf("else Int ==> %s %d", query, v.Field(i).Int())
				}
			case reflect.String:
				if i == 0 {
					query = fmt.Sprintf("else String ==> %s %s", query, v.Field(i).String())
				} else {
					query = fmt.Sprintf("else String ==> %s %s", query, v.Field(i).String())
				}
			default:
				fmt.Println("Unsupported type")
                return
			}
		}
		query = fmt.Sprintf("%s)", query)
		fmt.Println(query)
		return
	}
	fmt.Println("unsupported type")
}
func createQuery(q interface{}) {
	// reflect.Type 表示 interface{} 的具体类型，而 reflect.Value 表示它的具体值。
	// eflect.TypeOf() 和 reflect.ValueOf() 两个函数可以分别返回 reflect.Type 和 reflect.Value。
	t := reflect.TypeOf(q)
	v := reflect.ValueOf(q)
	aaa := t.Kind()
	fmt.Println("Type: ", t)
	fmt.Println("ValueOf: ", v)
	fmt.Println("Kind: ", aaa)
}
func createQuery1(q interface{}) {
	if(reflect.ValueOf(q).Kind() == reflect.Struct) {
		v := reflect.ValueOf(q)
		fmt.Println("Number of fields", v.NumField())
		for i := 0; i < v.NumField(); i++ {
			fmt.Printf("fieId:%d tyep:%T value:%v\n",i ,v.Field(i), v.Field(i))
		}
	}
}
func main() {
	o1 := order{ordId: 100, customerId: 560}
	createQuery(o1)

	//NumField() 方法返回结构体中字段的数量，而 Field(i int) 方法返回字段 i 的 reflect.Value。
	createQuery1(o1)

	e1 := employee {
		name: "yanghao",
		id: 123,
		address: "Coimbatore",
		salary: 999999,
		country: "India",
	}
	fmt.Println("============= o1 ==============")
	createQuery2(o1)
	fmt.Println("============= e1 ==============")
	createQuery2(e1)
	fmt.Println("============= i1 ==============")
	i1 := 90
	createQuery2(i1)


}